package com.baizw.cultivation.module.admin.mapstruct;

import cn.hutool.core.collection.CollUtil;
import com.baizw.cultivation.module.admin.entity.ResourceEntity;
import com.baizw.cultivation.module.admin.entity.vo.ResourceTreeSelect;
import com.baizw.cultivation.module.admin.entity.vo.UserMenu;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.springframework.stereotype.Component;

/**
 * 菜单转换
 *
 * @author 剧终1204
 * @version V1.0
 * @date 2020年06月09日 11:43
 */
@Mapper(componentModel = "spring")
public interface UserMenuMapStruct {

    /**
     * 资源转菜单
     *
     * @param resource
     * @return
     */
    @Mappings({
            @Mapping(source = "title", target = "meta.title"),
            @Mapping(source = "icon", target = "meta.icon"),
            @Mapping(source = "target", target = "meta.target"),
            @Mapping(source = "show", target = "meta.show"),
            @Mapping(source = "keepAlive", target = "meta.keepAlive")
    })
    UserMenu convert(ResourceEntity resource);

    /**
     * 转化下拉树
     *
     * @param resource
     * @return
     */
    @Mappings({
            @Mapping(source = "id", target = "value"),
            @Mapping(source = "parentId", target = "pId"),
            @Mapping(expression = "java(com.baizw.cultivation.module.admin.mapstruct.UserMenuMapStruct.isLeafExpression(resource))", target = "leaf")
    })
    ResourceTreeSelect covertSelect(ResourceEntity resource);

    /**
     * 判断该节点是否是子节点
     *
     * @param resource 资源
     * @return 是/不是
     */
    static boolean isLeafExpression(ResourceEntity resource) {
        if (resource == null) {
            return false;
        }
        return CollUtil.isEmpty(resource.getChildren());
    }
}
